<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="110px">
      <el-form-item label="住院号" prop="hosptilIdcard">
        <el-input
          v-model="queryParams.hosptilIdcard"
          placeholder="请输入住院号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="紧急联系人号码" prop="contactPerson1">
        <el-input
          v-model="queryParams.contactPerson1"
          placeholder="请输入紧急联系人号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="入院时间" prop="hosptilIntime">
        <el-date-picker clearable
          v-model="queryParams.hosptilIntime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择入院时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="出院时间" prop="hosptilOuttime">
        <el-date-picker clearable
          v-model="queryParams.hosptilOuttime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择出院时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['hospital:hosptilnews:add']"-->
<!--        >入院窗口</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['hospital:hosptilnews:edit']"
        >修改</el-button>
      </el-col>-->
<!--      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['hospital:hosptilnews:remove']"
        >删除</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['hospital:hosptilnews:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="hosptilnewsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="病人ID" align="center" prop="hosptilId" v-if="false"/>
      <el-table-column label="住院号" align="center" prop="hosptilIdcard" width="85"/>
<!--      <el-table-column label="家庭住址" align="center" prop="hosptilPatientadd" />-->
      <el-table-column label="紧急联系人" align="center" prop="contactPerson1" width="108"/>
<!--      <el-table-column label="紧急联系人2" align="center" prop="contactPerson2" />-->
      <el-table-column label="病人状态" align="center" prop="hosptilStatus" />
      <el-table-column label="入院时间" align="center" prop="hosptilIntime" width="170">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.hosptilIntime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="出院时间" align="center" prop="hosptilOuttime" width="170">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.hosptilOuttime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="客户病例表id" align="center" prop="patientId" />-->
      <el-table-column label="病人姓名" align="center" prop="patientName" />
<!--      <el-table-column label="住院医生id" align="center" prop="docterId" />-->
      <el-table-column label="医生" align="center" prop="docterName" />
<!--      <el-table-column label="护士id" align="center" prop="docter2Id" />-->
      <el-table-column label="护士" align="center" prop="docterName2" />
<!--      <el-table-column label="床位id" align="center" prop="bedId" />-->
      <el-table-column label="床位" align="center" prop="bedName" />
<!--      <el-table-column label="科室id" align="center" prop="deptId" />-->
      <el-table-column label="科室" align="center" prop="deptName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template slot-scope="scope">
          <el-button
            size="small"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['hospital:hosptilnews:edit']"
          >更改</el-button>
          <el-button
            size="small"
            type="danger"
            @click="handleDelete2(scope.row)"
            v-hasPermi="['hospital:hosptilnews:remove']"
          >出院</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改住院信息表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="400px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
<!--        <el-form-item label="住院号" prop="hosptilIdcard">
          <el-input v-model="form.hosptilIdcard" placeholder="请输入住院号" />
        </el-form-item>-->
<!--        <el-form-item label="家庭住址" prop="hosptilPatientadd">
          <el-input v-model="form.hosptilPatientadd" placeholder="请输入家庭住址" size="small"/>
        </el-form-item>
        <el-form-item label="紧急联系人1" prop="contactPerson1">
          <el-input v-model="form.contactPerson1" placeholder="请输入紧急联系人1" size="small"/>
        </el-form-item>
        <el-form-item label="紧急联系人2" prop="contactPerson2">
          <el-input v-model="form.contactPerson2" placeholder="请输入紧急联系人2" size="small"/>
        </el-form-item>-->
<!--        <el-form-item label="入院时间" prop="hosptilIntime">
          <el-date-picker clearable
            v-model="form.hosptilIntime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择${comment}">
          </el-date-picker>
        </el-form-item>-->
<!--        <el-form-item label="出院时间" prop="hosptilOuttime">
          <el-date-picker clearable
            v-model="form.hosptilOuttime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择${comment}">
          </el-date-picker>
        </el-form-item>-->
<!--        <el-form-item label="病人病历编号" prop="patientId">
          <el-input v-model="form.patientId" placeholder="请输入病人病历编号" size="small"/>
        </el-form-item>-->

<!--        <el-form-item label="科室id" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入${comment}" />
        </el-form-item>-->
        <el-form-item label="科室" :label-width="formLabelWidth">
          <el-select v-model="form.deptId" placeholder="请选择科室" @change="filtrate(form.deptId)" size="small">
            <el-option
              v-for="dept in deptList"
              :key="dept.value"
              :label="dept.deptName"
              :value="dept.deptId">
            </el-option>
          </el-select>
        </el-form-item>
<!--        <el-form-item label="住院医生id" prop="docterId">
          <el-input v-model="form.docterId" placeholder="请输入${comment}" />
        </el-form-item>-->
        <el-form-item label="医生" prop="docterId" >
          <el-select v-model="form.docterId" placeholder="请选择住院医生" size="small">
            <el-option
              v-for="item in doctorList"
              :key="item.value"
              :label="item.doctorName"
              :value="item.doctorId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="护士" prop="docter2Id">
          <el-select v-model="form.docter2Id" placeholder="请选择护士" size="small">
            <el-option
              v-for="item in nurseList"
              :key="item.value"
              :label="item.doctorName"
              :value="item.doctorId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="床位" prop="bedId">
          <el-select v-model="form.bedId" placeholder="请选择床位" size="small">
            <el-option
              v-for="item in bedList"
              :key="item.value"
              :label="item.bedName"
              :value="item.bedId">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listHosptilnews, getHosptilnews, delHosptilnews, delHosptilnews2, addHosptilnews, updateHosptilnews, listDepts, listDoctor } from "@/api/hospital/hosptilnews";

export default {
  name: "Hosptilnews",
  data() {
    return {
      deptList: [],//所有的科室
      doctorList: [],//科室对应的医生
      nurseList: [],//科室对应的护士
      bedList: [],//科室对应的床位
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 住院信息表表格数据
      hosptilnewsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        hosptilIdcard: null,
        hosptilPatientadd: null,
        contactPerson1: null,
        contactPerson2: null,
        hosptilStatus: null,
        hosptilIntime: null,
        hosptilOuttime: null,
        patientId: null,
        docterId: null,
        docter2Id: null,
        bedId: null,
        deptId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.loadDepts();
    //this.handleAdd();
  },
  methods: {
    /** 根据科室选择对应的医生、护士、床位 */
    filtrate(deptId){
      listDoctor(deptId).then(response => {
        this.doctorList = response.data.doctorList;
        this.nurseList = response.data.nurseList;
        this.bedList = response.data.bedList;
      });
    },
    /** 查询所有的科室 */
    loadDepts() {
      listDepts().then(response => {
        this.deptList = response.data.deptList;
      });
    },
    /** 查询住院信息表列表 */
    getList() {
      this.loading = true;
      listHosptilnews(this.queryParams).then(response => {
        this.hosptilnewsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        hosptilId: null,
        hosptilIdcard: null,
        hosptilPatientadd: null,
        contactPerson1: null,
        contactPerson2: null,
        hosptilStatus: null,
        hosptilIntime: null,
        hosptilOuttime: null,
        patientId: null,
        docterId: null,
        docter2Id: null,
        bedId: null,
        deptId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.hosptilId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "住院病人信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const hosptilId = row.hosptilId || this.ids
      getHosptilnews(hosptilId).then(response => {
        this.form = response.data.xx;
        this.doctorList = response.data.doctorList;
        this.nurseList = response.data.nurseList;
        this.bedList = response.data.bedList;
        this.open = true;
        this.title = "更改病房信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.hosptilId != null) {
            updateHosptilnews(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHosptilnews(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const hosptilIds = row.hosptilId || this.ids;
      this.$modal.confirm('是否确认删除住院信息表编号为"' + hosptilIds + '"的数据项？').then(function() {
        return delHosptilnews(hosptilIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 出院 */
    handleDelete2(row) {
      const hosptilIds = row.hosptilId || this.ids;
      this.$modal.confirm('确认是否出院').then(function() {
        return delHosptilnews2(hosptilIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功,请尽快缴费");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('hospital/hosptilnews/export', {
        ...this.queryParams
      }, `hosptilnews_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
